//
//  PhotoSelector.m
//  SXH-Bridge
//
//  Created by sxh on 2020/9/10.
//  Copyright © 2020 yuxin. All rights reserved.
//

#import "PhotoSelector.h"
#import "NSObject+UIViewController.h"

@import ZLPhotoBrowser;

@interface PhotoSelector()

@property (nonatomic, strong) ZLPhotoPreviewSheet *photoActionSheet;
//@property (nonatomic, strong) ZLPhotoActionSheet *photoActionSheet;

@end

@implementation PhotoSelector

+ (instancetype)shareInstance {
    static PhotoSelector *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[PhotoSelector alloc] init];
        shared.maxSelectCount = 9;
        shared.saveNewImageAfterEdit = NO;
        shared.maxEditVideoTime = 20;
        [shared initPhotos];
    });
    return shared;
}

- (void)initPhotos {
    [self photoActionSheet];
}

///打开相机
- (void)openCamera:(PhotoCallBack)completeHandle {
    ZLCameraConfiguration *cameraConfig = [ZLPhotoConfiguration default].cameraConfiguration;
    // All properties of the camera configuration have default value
    cameraConfig.sessionPreset = CaptureSessionPresetHd1920x1080;
    cameraConfig.focusMode = FocusModeContinuousAutoFocus;
    cameraConfig.exposureMode = ExposureModeContinuousAutoExposure;
    cameraConfig.flashMode = FlashModeOff;
    cameraConfig.videoExportType = VideoExportTypeMp4;

    ZLCustomCamera *camera = [[ZLCustomCamera alloc] init];
    camera.takeDoneBlock = ^(UIImage * _Nullable image, NSURL * _Nullable videoUrl) {
        NSLog(@"image:%@-------videoUrl:%@",image, videoUrl);
        if (image) {
            [self saveImage:image :^(BOOL succ, PHAsset *asset) {
                NSLog(@"succ:%d----asset:%@",succ, asset);
            }];
            completeHandle(@[image],nil);
        }
        if (videoUrl) {
            [self saveVideo:videoUrl :^(BOOL succ, PHAsset *asset) {
                NSLog(@"succ:%d----asset:%@",succ, asset);
            }];
            completeHandle(@[], nil);
        }
    };
    [self.zlzk_viewController presentViewController:camera animated:YES completion:nil];

//    ZLCustomCamera *camera = [[ZLCustomCamera alloc] init];
//    camera.allowRecordVideo = YES;
//    camera.doneBlock = ^(UIImage *image, NSURL *videoUrl) {
//        NSLog(@"image:%@-------videoUrl:%@",image, videoUrl);
//        if (image) {
//            [self saveImage:image :^(BOOL succ, PHAsset *asset) {
//                NSLog(@"succ:%d----asset:%@",succ, asset);
//            }];
//            completeHandle(@[image],nil);
//        }
//        if (videoUrl) {
//            [self saveVideo:videoUrl :^(BOOL succ, PHAsset *asset) {
//                NSLog(@"succ:%d----asset:%@",succ, asset);
//            }];
//            completeHandle(@[], nil);
//        }
//    };
//    [self.zlzk_viewController presentViewController:camera animated:YES completion:nil];
}

///打开相册
- (void)openPhotos:(PhotoCallBack)completeHandle {
    ZLPhotoPreviewSheet *ps = [[ZLPhotoPreviewSheet alloc] initWithSelectedAssets:@[]];
    ps.selectImageBlock = ^(NSArray<ZLResultModel *> * _Nonnull images, BOOL isOriginal) {
        NSMutableArray *imageList = @[].mutableCopy;
        [images enumerateObjectsUsingBlock:^(ZLResultModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [imageList addObject:obj.image];
        }];
        completeHandle(imageList, nil);
    };
//    ps.selectImageBlock = ^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
//        NSLog(@"i-%@,,,url:%@",images,assets);
//        // your code
//        completeHandle(images,nil);
//    };
    [ps showPreviewWithAnimate:YES sender:self.zlzk_viewController];
//    self.photoActionSheet.configuration.maxSelectCount = self.maxSelectCount;
//    self.photoActionSheet.sender = self.zlzk_viewController;
//
//    [self.photoActionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nullable images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
//        completeHandle(images,nil);
//    }];
//
//    [self.photoActionSheet showPreviewAnimated:YES];
}

- (void)saveImage:(UIImage *)image :(void(^)(BOOL succ,PHAsset *asset))complete {
    [ZLPhotoManager saveImageToAlbumWithImage:image completion:^(BOOL succ, PHAsset * _Nullable asset) {
        complete(succ, asset);
    }];
//    [ZLPhotoManager saveImageToAblum:image completion:^(BOOL suc, PHAsset * _Nonnull asset) {
//        complete(suc,asset);
//    }];
}

- (void)saveVideo:(NSURL *)url :(void(^)(BOOL succ,PHAsset *asset))complete {
    [ZLPhotoManager saveVideoToAlbumWithUrl:url completion:^(BOOL succ, PHAsset * _Nullable asset) {
        complete(succ, asset);
    }];
//    [ZLPhotoManager saveVideoToAblum:url completion:^(BOOL suc, PHAsset * _Nonnull asset) {
//        complete(suc, asset);
//    }];
}

#pragma mark - getters && setters
//- (ZLPhotoActionSheet *)photoActionSheet {
//    if (!_photoActionSheet) {
//        _photoActionSheet = [[ZLPhotoActionSheet alloc] init];
//        _photoActionSheet.configuration.maxSelectCount = _maxSelectCount;
//        _photoActionSheet.configuration.allowSelectGif = YES;
//        _photoActionSheet.configuration.allowSelectVideo = YES;
//        _photoActionSheet.configuration.allowSelectLivePhoto = NO;
//        _photoActionSheet.configuration.allowEditVideo = YES;
//        _photoActionSheet.configuration.editAfterSelectThumbnailImage = NO;
//        _photoActionSheet.configuration.saveNewImageAfterEdit = _saveNewImageAfterEdit;
//        _photoActionSheet.configuration.maxEditVideoTime = _maxEditVideoTime;
//        _photoActionSheet.sender = self.zlzk_viewController;
//    }
//    return _photoActionSheet;
//}
- (ZLPhotoPreviewSheet *)photoActionSheet {
    if (!_photoActionSheet) {
        [ZLPhotoConfiguration default].allowMixSelect = _maxSelectCount;
        [ZLPhotoConfiguration default].allowSelectGif = YES;
        [ZLPhotoConfiguration default].allowSelectVideo = YES;
        [ZLPhotoConfiguration default].allowEditImage = YES;
        [ZLPhotoConfiguration default].editAfterSelectThumbnailImage = NO;
        [ZLPhotoConfiguration default].saveNewImageAfterEdit = _saveNewImageAfterEdit;
        [ZLPhotoConfiguration default].maxEditVideoTime = 20;
        _photoActionSheet = [[ZLPhotoPreviewSheet alloc] initWithSelectedAssets:@[]];
    }
    return _photoActionSheet;
}

- (void)setMaxSelectCount:(NSInteger)maxSelectCount {
    _maxSelectCount = maxSelectCount;
    [ZLPhotoConfiguration default].allowMixSelect = maxSelectCount;
//    self.photoActionSheet.configuration.maxSelectCount = maxSelectCount;
}

- (void)setSaveNewImageAfterEdit:(BOOL)saveNewImageAfterEdit {
    _saveNewImageAfterEdit = saveNewImageAfterEdit;
    [ZLPhotoConfiguration default].saveNewImageAfterEdit = saveNewImageAfterEdit;
//    self.photoActionSheet.configuration.saveNewImageAfterEdit = saveNewImageAfterEdit;
}

- (void)setMaxEditVideoTime:(NSInteger)maxEditVideoTime {
    _maxEditVideoTime = maxEditVideoTime;
    [ZLPhotoConfiguration default].maxEditVideoTime = maxEditVideoTime;
//    self.photoActionSheet.configuration.maxEditVideoTime = maxEditVideoTime;
}

@end
